
module AtomMap where

import Data.Map
import Data.IntMap

-- special map for the Ints -> Atoms capable of reverse lookup and assigning new atoms
-- also used for clauseIDS

data AtomMap str = ATM { foremap :: IntMap str,
			 revmap :: Map str Key,
			 maxalloc :: Key }

newAtom :: (Ord a) => a -> AtomMap a -> (Key,AtomMap a)
newAtom str am = 
    maybe (num,
           am { foremap = Data.IntMap.insert num str (foremap am),
		revmap = Data.Map.insert str num (revmap am),
		maxalloc = 1 + (maxalloc am) })
	  (\x -> (x,am))
	  (AtomMap.lookupAtomRef str am)
    where num = maxalloc am

empty :: (Ord a) => AtomMap a 
empty = ATM {foremap = Data.IntMap.empty, revmap = Data.Map.empty, maxalloc = 0}

lookupAtomRef :: (Ord a) => a -> AtomMap a -> Maybe Key
lookupAtomRef str am = Data.Map.lookup str (revmap am)

lookupAtom :: (Ord a) => Key -> AtomMap a -> Maybe a
lookupAtom key am = Data.IntMap.lookup key (foremap am)



